/**********************************************************************\

 Copyright 2012 Diggory Blake

 This file is part of klip-engine

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

\**********************************************************************/

#include "kinput.h"

keButtonState kBasicMouseInput::getMouseButtons(keMouseButtons buttons) {
	return makeButtonState((_prevButtons & buttons) == buttons, (_buttons & buttons) == buttons);
}
kVec2i kBasicMouseInput::getMousePos() {
	return _pos;
}
kVec2i kBasicMouseInput::getMouseMove() {
	return _pos - _prevPos;
}
kVec2 kBasicMouseInput::getMouseMoveRaw() {
	return _rawMove;
}
int kBasicMouseInput::getMouseWheel() {
	return _scroll;
}
bool kBasicMouseInput::getMouseOver() {
	return _over;
}
void kBasicMouseInput::updateMousePos(const kVec2i& pos) {
	if (pos != _pos) {
		_pos = pos;
		CALL_DELEGATE(onMouseMove, _sender, _pos);
	}
}
void kBasicMouseInput::updateMouseButtons(keMouseButtons buttons, bool down) {
	if (down) {
		if ((_buttons & buttons) != buttons) {
			_buttons |= buttons;
			CALL_DELEGATE(onMouseDown, _sender, buttons);
		}
	} else {
		if ((_buttons & buttons) == buttons) {
			_buttons &= ~buttons;
			CALL_DELEGATE(onMouseUp, _sender, buttons);
		} else {
			_buttons &= ~buttons;
		}
	}
}
void kBasicMouseInput::updateMouseWheel(int scroll) {
	if (scroll) {
		_scroll += scroll;
		CALL_DELEGATE(onMouseWheel, _sender, scroll);
	}
}
void kBasicMouseInput::updateMouseOver(bool over) {
	if (over != _over) {
		_over = over;
		if (_over) {
			CALL_DELEGATE(onMouseEnter, _sender);
		} else {
			CALL_DELEGATE(onMouseLeave, _sender);
		}
	}
}

keButtonState kBasicKeyboardInput::getKey(keKeyCode code) {
	return makeButtonState(_prevKeys[code], _keys[code]);
}
void kBasicKeyboardInput::updateKey(keKeyCode code, bool down) {
	if (_keys[code] != down) {
		_keys[code] = down;
		if (down) {
			CALL_DELEGATE(onKeyDown, _sender, code);
		} else {
			CALL_DELEGATE(onKeyUp, _sender, code);
		}
	}
}
